package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.utils.BitmapUtils;

public class MyFriendListExpandableAdapter extends BaseExpandableListAdapter {

	private LayoutInflater inflater = null;
	private final ImageLoader mImageLoader;
	private String[] arrayName ;
	private Bitmap bDown;
	private Bitmap bUp;
	private Bitmap bg;
	private BitmapDrawable bg_draw;
	private OnClickFriendHeaderListener mOnClickFriendHeaderListener;
	private OnClickFriendAttentionListener mOnClickFriendAttentionListener;
	private AppData appData;
	private int mFansMessageCount;
	private List<String> mInterestFriendIdList=new ArrayList<String>();
	
	public MyFriendListExpandableAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		mImageLoader = ImageLoader.getInstance();
		Resources resources = context.getResources();
		bDown = BitmapUtils.getBackGroundBitmap(resources,R.drawable.friend_group_arrows_down);
		bUp = BitmapUtils.getBackGroundBitmap(resources,R.drawable.friend_group_arrows);
		bg = BitmapUtils.getBackGroundBitmap(resources,R.drawable.setting_title_bg);
		bg_draw = new BitmapDrawable(bg);
		appData = HTalkApplication.getAppData();
		if(SmartFoxClient.getLoginUserInfo().isDoctor()){
			arrayName = new String[] { "我关注的朋友", "关注我的朋友", "我的黑名单" };
		}else{
			arrayName = new String[] { "我关注的病友", "关注我的病友", "我的黑名单" };
		}
	}

	
	public void changeData(List<String> interestFriendIdList){
		mInterestFriendIdList.clear();
		mInterestFriendIdList.addAll(interestFriendIdList);
		notifyDataSetChanged();
	}
	
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// 网格加载一次
		if (groupPosition == 1 || groupPosition == 2) {
			return 1;
		} else {
			return mInterestFriendIdList
					.size();
		}
	}

	public int getCount(int groupPosition) {
		if (groupPosition == 1 || groupPosition == 2) {
			return 1;
		} else {
			return mInterestFriendIdList
					.size();
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}
	
	public void  setFansMessageCount(int mFansMessageCount){
		this.mFansMessageCount  = mFansMessageCount;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		viewHolder holder;
		if (convertView == null || convertView.getTag() == null) {
			holder = new viewHolder();
			convertView = inflater.inflate(R.layout.knowledge_detail_expand_group, null);
			holder.mGroupName = (TextView) convertView.findViewById(R.id.expand_group_text);
			holder.mGroupNumber = (TextView) convertView.findViewById(R.id.group_number);
			holder.mImageIcon = (ImageView) convertView.findViewById(R.id.expand_group_head);
			convertView.setTag(holder);
		}
		holder = (viewHolder) convertView.getTag();
		holder.mGroupName.setText(arrayName[groupPosition]);
		if (groupPosition < 1) {
			holder.mGroupNumber.setText("("+String.valueOf(getCount(groupPosition)+")"));
		} else if (groupPosition == 1) {
			holder.mGroupNumber.setText("("+appData.attentionUserSize	+ ")");
		} else if (groupPosition == 2) {
			holder.mGroupNumber.setText("("+appData.blackListSize + ")");
		}

		convertView.setBackgroundDrawable(bg_draw);
		if (!isExpanded) {
			holder.mImageIcon.setImageBitmap(bUp);
		} else {
			holder.mImageIcon.setImageBitmap(bDown);
		}
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		viewHolder holder = null;
		if (groupPosition == 0) {
			if (convertView == null || convertView.getTag() == null) {
				holder = new viewHolder();
				convertView = inflater.inflate(R.layout.my_friend_item, null);
				holder.head = (ImageView) convertView.findViewById(R.id.head_image);
				holder.personName = (TextView) convertView.findViewById(R.id.name);
				holder.headSex = (ImageView) convertView.findViewById(R.id.head_sex);
				holder.v = (ImageView) convertView.findViewById(R.id.levl);
				holder.is_friend = (ImageView) convertView.findViewById(R.id.is_friend);
				holder.note = (TextView) convertView.findViewById(R.id.note);
				holder.message = (TextView) convertView.findViewById(R.id.item_dele);
				// holder.itemLine =
				// (ImageView)convertView.findViewById(R.id.item_line);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
		} else if (groupPosition == 1 || groupPosition == 2) {
			convertView = inflater.inflate(R.layout.fans_blacklist_item, null);
			TextView personName = (TextView) convertView.findViewById(R.id.name);
			ImageView head = (ImageView) convertView.findViewById(R.id.head_image);
			TextView mMessageCountTv = (TextView)convertView.findViewById(R.id.item_dele);
			// ImageView itemLine =
			// (ImageView)convertView.findViewById(R.id.item_line);
			if (groupPosition == 1) {
				head.setImageResource(R.drawable.fans);
				personName.setText("关注我的人"+ "("+appData.attentionUserSize+ ")");
				// itemLine.setVisibility(View.GONE);
				if (mFansMessageCount > 0) {
					mMessageCountTv.setVisibility(View.GONE);
					mMessageCountTv.setText(mFansMessageCount+"");
				}else {
					mMessageCountTv.setVisibility(View.GONE);
				}
			} else {
				head.setImageResource(R.drawable.blacklist);
				personName.setText("我的黑名单" + "("+ appData.blackListSize + ")");
				// itemLine.setVisibility(View.VISIBLE);
				mMessageCountTv.setVisibility(View.GONE);
			}
			return convertView;
		}
		// //是否去掉分割线
		// if (isLastChild) {
		// holder.itemLine.setVisibility(View.GONE);
		// }else {
		// holder.itemLine.setVisibility(View.VISIBLE);
		// }

		final CustomerInfoEntity mInfoEntity = (CustomerInfoEntity) appData.getCustomerInfoEntity(
						mInterestFriendIdList.get(childPosition));
		// 头像
		mImageLoader.displayImage(mInfoEntity.getSex(),mInfoEntity.getNormalHeadIcon(), holder.head);
		holder.head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (mOnClickFriendHeaderListener != null) {
					mOnClickFriendHeaderListener.onClickFriendHeader(groupPosition, childPosition, mInfoEntity);
				}
			}
		});
		// 姓名
		holder.personName.setText(mInfoEntity.getName());
		holder.headSex.setImageLevel(Integer.valueOf(mInfoEntity.getSex()));
		// 是否是医师沙龙
		if (mInfoEntity.isDoctor()) {
			holder.v.setVisibility(View.VISIBLE);
		} else {
			holder.v.setVisibility(View.GONE);
		}
		// 签名
		holder.note.setText(mInfoEntity.getDescription());
		// 消息
		holder.message.setVisibility(View.VISIBLE);
		int size = appData.getNoReadMesgSize(mInfoEntity.getId());
		if (appData.getNoReadMesgSize(mInfoEntity.getId()) != 0) {
			holder.message.setText(size + "");
		} else {
			holder.message.setVisibility(View.GONE);
		}
		
		holder.is_friend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mOnClickFriendAttentionListener!=null)
					mOnClickFriendAttentionListener.onClickFriendAttention(groupPosition, childPosition, mInfoEntity);
			}
		});
		
		if(mInfoEntity.getIsAttentionFriend()==1){//是否关注
			holder.is_friend.setBackgroundResource(R.drawable.not_attention);
		}else{
			holder.is_friend.setBackgroundResource(R.drawable.attention);
		}
		
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}


	private static class viewHolder {
		private TextView note;// 描述
		private ImageView head; // 头像
		// private TextView messageNum;//信息数量
		private TextView personName; // 名称
		private ImageView headSex; // 性别
		private ImageView v;
		private ImageView is_friend;//关注 / 取消关注
		private TextView message;// 消息的图标
		private ImageView mImageIcon;
		private TextView mGroupNumber;
		private TextView mGroupName;
	}

	public interface OnClickFriendHeaderListener {
		void onClickFriendHeader(int groupPosition, int childPosition,
				CustomerInfoEntity mInfoEntity);
	}

	public void setOnClickFriendHeaderListener(
			OnClickFriendHeaderListener mOnClickFriendHeaderListener) {
		this.mOnClickFriendHeaderListener = mOnClickFriendHeaderListener;
	}

	public void setOnClickFriendAttentionListener(
			OnClickFriendAttentionListener mOnClickFriendAttentionListener) {
		this.mOnClickFriendAttentionListener = mOnClickFriendAttentionListener;
	}

	public interface OnClickFriendAttentionListener {
		void onClickFriendAttention(int groupPosition, int childPosition,
				CustomerInfoEntity mInfoEntity);
	}

}
